package com.cn.xly2023.controller;


import com.cn.xly2023.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传{}",image);
        //获取文件原始名
        String originalFilename = image.getOriginalFilename();
        int index = originalFilename.lastIndexOf(".");
        //获取文件扩展名
        String extname = originalFilename.substring(index);
        String newFileName = UUID.randomUUID() + extname;
        //将文件存储到磁盘目录F:\image,文件名为 uuid + 文件扩展名
        image.transferTo(new File("F:/image/" + newFileName));
        String url = "image/" + newFileName;
        System.out.println(url);
        return Result.success(url);
    }
}
